package com.qding.park.manager.app.queue.handle;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.qding.park.common.core.cache.service.CacheServiceFacade;
import com.qding.park.common.core.exceptions.BaseException;
import com.qding.park.common.core.exceptions.IExceptionHandle;
import com.qding.park.common.utils.DateUtils;
import com.qding.park.manager.pojo.TbMonthRent;
import com.qding.park.manager.pojo.TbReserve;
import com.qding.park.manager.pojo.queue.QueueMsg;

/**
 * @author Administrator
 *清除过期订单缓存
 */
@Component("clearTimeOutOrderHandle")
public class ClearTimeOutOrderHandle extends AbstractAllHandle {

	private static final int timeout_minute = 30;

	@Override
	void actualHandle(QueueMsg msg) throws BaseException {
		// 清除临停订单缓存数据
		List<TbReserve> reserveList = cacheServiceFacade.tempPay_cache_getAll();
		for (TbReserve tbReserve : reserveList) {
			if (tbReserve.getCreate_date() == null) {
				continue;
			}
			if (DateUtils.addMinute(tbReserve.getCreate_date(), timeout_minute).getTime() < new Date().getTime()) {
				// 过期
				cacheServiceFacade.tempPay_cache_delete(tbReserve);
			}
		}

		List<TbMonthRent> monthRentList = cacheServiceFacade.monthrentPay_cache_getAll();
		for (TbMonthRent tbMonthRent : monthRentList) {
			if (tbMonthRent.getCreate_date() == null) {
				continue;
			}
			if (DateUtils.addMinute(tbMonthRent.getCreate_date(), timeout_minute).getTime() < new Date().getTime()) {
				// 过期
				cacheServiceFacade.monthrentPay_cache_delete(tbMonthRent.getId());
			}
		}

	}

	@Override
	boolean failDiscardRollBacK(QueueMsg msg) {
		_log.error("清除过期订单缓存任务回滚:"+msg);
		return true;
	}

}
